
// MultiMediaDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MultiMedia.h"
#include "MultiMediaDlg.h"
#include <commctrl.h>
#include <commdlg.h>
#include <stdio.h>
#include <strsafe.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	enum { IDD = IDD_ABOUTBOX };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

// Implementation
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()


// CMultiMediaDlg dialog

CMultiMediaDlg *gInstance;
CMultiMediaDlg* CMultiMediaDlg::ShareInstance()
{
	return gInstance;
}

CMultiMediaDlg::CMultiMediaDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMultiMediaDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	gInstance = this;
}

void CMultiMediaDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CMultiMediaDlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_WM_SIZE()
	ON_WM_ERASEBKGND()
	ON_COMMAND(ID_FILE_OPENFILE, &CMultiMediaDlg::OnFileOpenfile)
END_MESSAGE_MAP()

//struct MediaStruct
//{
//	int ID;
//	CString FilePath;
//	CString FileName;
//};
//MediaStruct *myMediaList = new MediaStruct[1000];

// CMultiMediaDlg message handlers

BOOL CMultiMediaDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	// TODO: Add extra initialization here
	m_Cmenu.LoadMenuW(IDR_MENU1);
	SetMenu(&m_Cmenu);

	CoInitializeEx(NULL,COINIT_APARTMENTTHREADED);

	RECT dlgRect; 
	GetWindowRect(&dlgRect);
	m_rectParentDlg = GetCRectFromRECT(dlgRect);

	m_gControlMedia.Create(NULL, NULL, WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, CRect(0,0,0,0), this, NULL);
	m_gControlMedia.ShowWindow(SW_SHOW);
	m_gControlMedia.SetWindowPos(NULL,0,dlgRect.bottom-dlgRect.top-150, dlgRect.right, dlgRect.bottom, SWP_NOZORDER);

	m_gListFileMultiMedia.Create(NULL, NULL, WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, CRect(0,0,0,0), this, NULL);
	m_gListFileMultiMedia.ShowWindow(SW_SHOW);
	m_gListFileMultiMedia.SetWindowPos(NULL,0,dlgRect.top, dlgRect.left+200, dlgRect.bottom-dlgRect.top-150, SWP_NOZORDER);
 
	m_gShowVideo.Create(NULL, NULL, WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, CRect(0,0,0,0), this, NULL);
	m_gShowVideo.ShowWindow(SW_SHOW);
	m_gShowVideo.SetWindowPos(NULL,dlgRect.left+200,dlgRect.top, dlgRect.right, dlgRect.bottom-dlgRect.top-150, SWP_NOZORDER);
	
	nCount = 0;
	m_bAvoidReplayWhenAddFile = false;
	m_nVol = -1;

	PostMessage(WM_GRAPHNOTIFY,NULL,NULL);	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMultiMediaDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CMultiMediaDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CMultiMediaDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}
CRect CMultiMediaDlg::GetCRectFromRECT(RECT lpRECT)
{
	CRect rtRect;
	rtRect.left = lpRECT.left;
	rtRect.top = lpRECT.top;
	rtRect.right = lpRECT.right;// - lpRECT.left;
	rtRect.bottom = lpRECT.bottom;// - lpRECT.top;
	return rtRect;
}

Rect CMultiMediaDlg::GetRectFromRECT(RECT lpRECT)
{
	Rect rtRect;
	rtRect.X = lpRECT.left;
	rtRect.Y = lpRECT.top;
	rtRect.Width = lpRECT.right - lpRECT.left;
	rtRect.Height = lpRECT.bottom - lpRECT.top;
	return rtRect;
}


void CMultiMediaDlg::OnSize(UINT nType, int cx, int cy)
{
	CDialog::OnSize(nType, cx, cy);
	RECT dlgRect;
	GetClientRect(&dlgRect);
	if(m_gControlMedia.m_hWnd != NULL||m_gListFileMultiMedia.m_hWnd!=NULL|m_gShowVideo.m_hWnd!=NULL)
	{  
	
		m_gControlMedia.SetWindowPos(NULL,0,dlgRect.bottom-dlgRect.top-150, dlgRect.right, dlgRect.bottom, SWP_NOZORDER);   
		m_gListFileMultiMedia.SetWindowPos(NULL,dlgRect.left,dlgRect.top, dlgRect.left+200, dlgRect.bottom-dlgRect.top-150, SWP_NOZORDER);
		m_gShowVideo.SetWindowPos(NULL,dlgRect.left+200,dlgRect.top, dlgRect.right, dlgRect.bottom-dlgRect.top-150, SWP_NOZORDER);
		//m_gSubTitle.SetWindowPos(NULL,0,dlgRect.bottom-dlgRect.top-250, dlgRect.right-200, dlgRect.bottom-dlgRect.top-150, SWP_NOZORDER);
	}
	//SendMessage(WM_GRAPHNOTIFY,NULL,NULL);
	
	// TODO: Add your message handler code here

}

BOOL CMultiMediaDlg::OnEraseBkgnd(CDC* pDC)
{
	// TODO: Add your message handler code here and/or call default
//	CRect rect;
//	GetClientRect(&rect);
////	CBrush myBrush(COLOR_VIEW_AREA);    // dialog background color
//	CBrush *pOld = pDC->SelectObject(&myBrush);
//	BOOL bRes  = pDC->PatBlt(0, 0, rect.Width(), rect.Height(), PATCOPY);
//	pDC->SelectObject(pOld);    // restore old brush

	return CDialog::OnEraseBkgnd(pDC);
}


void CMultiMediaDlg::OnFileOpenfile()
{
	// TODO: Add your command handler code here
	CString strFilter = _T("All files| *.mp3;*.wma;*.avi;*.wmv;*.flv;*.mkv;*.mov;*.mp4|");
	strFilter += _T("video file (*.avi;*.wmv;*.flv;*.mkv;*.mov;*.mp4)|*.avi;*.wmv;*.flv;*.mkv;*.mov;*.mp4|");
	strFilter += _T("audio file (*.mp3;*.wma)|*.mp3;*wma|");
	
	CFileDialog dlgFile(TRUE, NULL, NULL, OFN_FILEMUSTEXIST| OFN_HIDEREADONLY |OFN_ALLOWMULTISELECT,strFilter, NULL);
	dlgFile.m_ofn.lpstrTitle= _T("Open Media file");
	if (dlgFile.DoModal() == IDOK) 
	{
		

		POSITION pos; 
		pos = dlgFile.GetStartPosition(); 
		while(pos)
		{
			m_strFilePath=dlgFile.GetNextPathName(pos);
			int curPos= 0;
			m_strFileName= m_strFilePath.Tokenize(L"\\",curPos);
			while (m_strFileName != "")
			{	  
				m_strFileName= m_strFilePath.Tokenize(_T("\\"),curPos);
				if(m_strFileName.Find(L".avi") != -1 || m_strFileName.Find(L".flv") != -1 || m_strFileName.Find(L".mkv") != -1
					||m_strFileName.Find(L".mov") != -1 || m_strFileName.Find(L".mp4") != -1 || m_strFileName.Find(L".mpg") != -1
					||m_strFileName.Find(L".wmv") != -1 || m_strFileName.Find(L".mp3") != -1 || m_strFileName.Find(L".wma") != -1)
					break;
			}
			//----------------Add data to MediaPlayList----------------
			m_gControlMedia.m_strFileName = m_strFileName;
			m_gControlMedia.m_strFilePath = m_strFilePath;
			MediaObject si(nCount,m_strFilePath,m_strFileName);
			arrMediaObjects.Add(si);
			m_gListFileMultiMedia.AddDataToPlayList(this->m_gListFileMultiMedia.m_listControlFile,nCount,0,arrMediaObjects[nCount].GetFileName());
			nCount++;
		}
	}

}
